package com.xpu.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpu.pms.comm.http.BasePage;
import com.xpu.pms.comm.http.PageInfo;
import com.xpu.pms.comm.http.QueryPage;
import com.xpu.pms.comm.http.Respond;
import com.xpu.pms.entity.SysUserEntity;
import com.xpu.pms.mapper.SysUserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/user")
public class SysUserController implements BasePage<SysUserEntity> {

    @Resource
    SysUserMapper sysUserMapper;


    @RequestMapping("/list")
    public List<SysUserEntity> list(){
        List<SysUserEntity> list =sysUserMapper.selectList(
                new LambdaQueryWrapper<>()
        );
        try {
            //休眠2秒,让页面上加载状态能够显示出来
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return list;
    }


    @PostMapping("/add")
    public Respond add(@RequestBody SysUserEntity user){
        user.setCreateDate(new Date());
        sysUserMapper.insert(user);


        try {
            //休眠2秒,让页面上加载状态能够显示出来
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Respond();
    }
    @PostMapping("/update")
    public Respond update(@RequestBody SysUserEntity user){
        sysUserMapper.updateById(user);


        return new Respond();
    }
    @RequestMapping("/delete")
    public Respond delete(@RequestBody SysUserEntity user){

        sysUserMapper.deleteById(user.getId());


        return new Respond();
    }


    /*@RequestMapping("/page")
    public Respond page(@RequestBody QueryPage page) {
        long index = page.getPage().getIndex();
        long size = page.getPage().getSize();
        IPage<SysUserEntity> ipage = sysUserMapper.selectPage(new Page<>(index,size),new QueryWrapper<>());
        PageInfo pageInfo = new PageInfo(ipage.getCurrent(),ipage.getTotal(),ipage.getSize());
        Respond res = new Respond().setPage(pageInfo).setCode(0).setData(ipage.getRecords());
        return  res;
    }*/

    @RequestMapping("/page")
    public Respond page(@RequestBody QueryPage page) {
        List<SysUserEntity> list = sysUserMapper.selectUserList(page.toMap());
        //查询表中行的总数
        int rowCount= sysUserMapper.selectCount(new QueryWrapper<>());
        PageInfo pi = new PageInfo(page.getPage().getIndex(),rowCount,page.getPage().getSize());
        return new Respond().setPage(pi).setData(list);
    }
}
